const util = require('../../../../../utils/util.js');
const api = require('../../../../../config/api.js');
const app = getApp();
Page({
  data: {
    journeyVideo:{},//视频数据
    selectAllLable: [],//选择的类目标签
    selectLableTextArray: [],// 选中的类目标签文字版
    openInput: false,//是否开启输入
    intro:'',//文字介绍
    addr: '',//地址简称
    addrLatitude: '',//地址纬度
    addrLongitude: '',//地址经度
    address: '',//地址全称
    privacy: false,//隐私状态
    titleOptions: 0,//关联和弹窗切换
    openRelevance: false,//关联面板
    openSetPopup: false,//设置弹窗面板
    relevanceType: 0,//关联类型选择
    relevanceIndex: null,//关联内容的Index
    relevanceInfo:'',//关联的数据
    setPopupInfo: '',//弹窗的数据
  },
  onLoad: function (options) {
    var _this = this;
    _this.setData({
      logininfo: app.globalData.logininfo,
      openid: app.globalData.logininfo.openid,
      journeyVideo: JSON.parse(options.info)
    });
    console.log(_this.data.journeyVideo)
  },
  //跳转到视频播放页面
  navigateToVideo: function (e) {
    var _this = this;
    console.log(e)
    var src = e.currentTarget.dataset.src;
    wx.navigateTo({
      url: "/pages/home/video/video?id=&state=0&type=2&src=" + src
    })
  },
  //打开输入
  showInput: function () {
    var _this = this
    this.setData({
      openInput: true,
    })
  },
  //关闭输入
  closeInput: function () {
    var _this = this
    this.setData({
      openInput: false,
    })
  },
  //获取输入的介绍
  inputIntro: function (e) {
    var _this = this
    var value = e.detail.value.replace(/\s+/g, '')
    _this.setData({
      intro: value,
    })
  },
  //打开选择地址组件
  openAddrPicker: function () {
    var _this = this
    wx.getSetting({
      success(res) {
        if (!res.authSetting['scope.userLocation']) {
          wx.authorize({
            scope: 'scope.userLocation',
            success() {
              _this.albumInfoPositionStart();
            },
            fail: function () {
            }
          })
        } else {
          _this.albumInfoPositionStart();
        }
      }
    })
  },
  albumInfoPositionStart: function () {
    var _this = this;
    wx.chooseLocation({
      success: function (res) {
        var value = res.name;
        var latitude = res.latitude;
        var longitude = res.longitude;
        var address = res.address;
        if (value != null && value.length > 0) {
          _this.setData({
            addr: value,
            addrLatitude: latitude,
            addrLongitude: longitude,
            address: address,
          })
        } else {
          util.dialogAlert("未选中有效地址")
        }
      }
    })
  },
  //隐私开关
  changePrivacy: function (e) {
    var _this = this
    _this.setData({
      privacy: e.detail.value
    })
  }, 
  //关联和弹窗切换
  selectTitleOptions: function(e){
    var _this = this
    var index = e.currentTarget.dataset.index;
    this.setData({
      titleOptions: index,
    })
  }, 
  //打开设置弹窗面板
  showSetPopup: function() {
    var _this = this
    this.setData({
      openSetPopup: true,
    })
  },
  //打开设置问答弹窗面板
  showSetPopupAnswer: function () {
    var _this = this
    this.setData({
      openSetPopupAnswer: true,
      openSetPopup: false
    })
  },
  //选择正确答案
  selectRightAnswers: function (e) {
    var _this = this
    var index = e.currentTarget.dataset.index;
    this.setData({
      rightAnswers: index,
    })
  },
  //获取问题的内容
  inputWen: function(e){
    var _this = this
    var value = e.detail.value.replace(/\s+/g, '')
    _this.setData({
      wenti: value
    })
  },
  //获取答案1的内容
  inputDa1: function (e) {
    var _this = this
    var value = e.detail.value.replace(/\s+/g, '')
    _this.setData({
      daan1: value
    })
  },
  //获取答案2的内容
  inputDa2: function (e) {
    var _this = this
    var value = e.detail.value.replace(/\s+/g, '')
    _this.setData({
      daan2: value
    })
  },
  //提交问答题
  submitAnswers: function () {
    var _this = this
    var wen = _this.data.wenti
    var da1 = _this.data.daan1
    var da2 = _this.data.daan2
    var rightAnswers = _this.data.rightAnswers
    if (wen != "" && wen!=null){
      if (da1 != "" && da1 != null && da2 != "" && da2!=null){
        if (rightAnswers != null && rightAnswers!=''){
          if (rightAnswers == 0) {
            var trueAnswers = da1
            var falseAnswers = da2
          } else {
            var trueAnswers = da2
            var falseAnswers = da1
          }
          var setPopupInfo = { "type": 2, "wen": wen, "trueAnswers": trueAnswers, "falseAnswers": falseAnswers }
          _this.setData({
            setPopupInfo: setPopupInfo,
            openSetPopupAnswer: false,
            wenti: '',
            daan1: '',
            daan2: '',
            rightAnswers: null,
          })
        }else{
          wx.showToast({
            title: '请设定正确答案',
            icon: 'none',
            duration: 1500
          })
        }
      }else{
        wx.showToast({
          title: '请完善两项答案',
          icon: 'none',
          duration: 1500
        })
      }
    }else{
      wx.showToast({
        title: '请填写完整问题',
        icon: 'none',
        duration: 1500
      })
    }
  },
  //打开设置驿站弹窗面板
  showSetPopupPost: function () {
    var _this = this
    this.setData({
      openSetPopupPost: true,
      openSetPopup: false
    })
  },
  //清空弹窗设置
  closeSetPopupInfo: function(){
    var _this = this
    this.setData({
      setPopupInfo: ''
    })
  },
  //打开关联面板
  showRelevance: function () {
    var _this = this
    this.setData({
      openRelevance: true,
    })
  },
  //选择关联类型
  selectRelevanceType: function(e){
    var _this = this
    var index = e.currentTarget.dataset.index;
    this.setData({
      relevanceType: index,
      relevanceIndex: null,
    })
  },
  //选中关联内容
  selectRelevance: function(e){
    var _this = this
    var index = e.currentTarget.dataset.index;
    this.setData({
      relevanceIndex: index,
    })
  },
  //确定关联内容
  submitSelectCover: function(){
    var _this = this
    var index = _this.data.relevanceIndex;
    _this.closeMC();
  },
  //保存草稿
  save: function(){
    var _this = this
    var lableList = _this.data.selectAllLable
    if (lableList != '' && lableList!=null){
      wx.redirectTo({
        url: '/grade/pages/grade/journey/journey',
      })
    } else {
      wx.showToast({
        title: '请选择类目',
        icon: 'none',
        duration: 1500
      })
    }
  },
  //发布
  publish: function(){
    var _this = this
    var lableList = _this.data.selectAllLable
    if (lableList != '' && lableList != null) {
      wx.redirectTo({
        url: '/grade/pages/grade/journey/journey',
      })
    } else {
      wx.showToast({
        title: '请选择类目',
        icon: 'none',
        duration: 1500
      })
    }
  },
  //跳转选择类目
  gotoSlectLable: function(){
    var _this = this
    wx.navigateTo({
      url: '/ucenter/pages/ucenter/personcenter/publish/itemlabel/itemlabel?type=journeyImg&selectAllLable=' + JSON.stringify(_this.data.selectAllLable)
    })
  },
  //关闭面板
  closeMC: function () {
    var _this = this
    _this.setData({
      openRelevance: false,
      openSetPopup: false,
      openSetPopupAnswer: false,
      openSetPopupPost: false,
    })
  },
})